﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLBH.ConfigLib
{
    public enum ConfiParam
    {
        WindowAuthentication = 0,
        ServerName = 1,
        UserName = 2,
        Pass = 3,                
        DBFileDirectory = 4,
        CreatedDB = 5        
    }
    public class QLBHSettings
    {
        private bool authen = false;//Windows authentication

        public bool Authen
        {
            get { return authen; }
            set { authen = value; }
        }
        private String userName = String.Empty;

        public String UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private String passWord = String.Empty;

        public String PassWord
        {
            get { return passWord; }
            set { passWord = value; }
        }
        private String serverName = String.Empty;

        public String ServerName
        {
            get { return serverName; }
            set { serverName = value; }
        }
        private String dbFileDirectory = String.Empty;

        public String DbFileDirectory
        {
            get { return dbFileDirectory; }
            set { dbFileDirectory = value; }
        }
        private bool createdDB = false;

        public bool CreatedDB
        {
            get { return createdDB; }
            set { createdDB = value; }
        }

        private const String TagName = "value";
        private const int ValueNumber = 0;

        private bool loadSetting = true;

        private static QLBHSettings instance;

        public static QLBHSettings Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new QLBHSettings();
                }
                if (instance.loadSetting)
                {
                    instance.LoadSettings();
                }
                return QLBHSettings.instance;
            }
        }

        public QLBHSettings()
        {

        }

        private void LoadSettings()
        {
            try
            {
                loadSetting = false;
                String projectName = Environment.CurrentDirectory;
                String fileName = projectName + "\\QLBH.exe.config";
                
                Xmlconfig config = new Xmlconfig(fileName, false);

                List<ConfigSetting> lConfig = (List<ConfigSetting>)config.Settings.GetNamedChildren("appSettings");
                List<ConfigSetting> SettingElements = (List<ConfigSetting>)lConfig[ValueNumber].GetNamedChildren("add");

                authen = SettingElements[(int)ConfiParam.WindowAuthentication].Value == "True" ? true : false;
                serverName=SettingElements[(int)ConfiParam.ServerName].Value;
                userName = SettingElements[(int)ConfiParam.UserName].Value;
                passWord = SettingElements[(int)ConfiParam.Pass].Value;
                dbFileDirectory = SettingElements[(int)ConfiParam.DBFileDirectory].Value;
                createdDB = SettingElements[(int)ConfiParam.CreatedDB].Value == "True" ? true : false;                

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public void SaveSettings()
        {
            try
            {
                loadSetting = true;

                String projectName = Environment.CurrentDirectory;
                String fileName = projectName + "\\QLBH.exe.config";

                Xmlconfig config = new Xmlconfig(fileName, false);

                List<ConfigSetting> lConfig = (List<ConfigSetting>)config.Settings.GetNamedChildren("appSettings");
                List<ConfigSetting> SettingElements = (List<ConfigSetting>)lConfig[ValueNumber].GetNamedChildren("add");

                SettingElements[(int)ConfiParam.WindowAuthentication].Value = authen == true ? "True" : "False";
                SettingElements[(int)ConfiParam.ServerName].Value = serverName;
                SettingElements[(int)ConfiParam.UserName].Value = userName;
                SettingElements[(int)ConfiParam.Pass].Value = passWord;
                SettingElements[(int)ConfiParam.DBFileDirectory].Value = dbFileDirectory;
                SettingElements[(int)ConfiParam.CreatedDB].Value = createdDB == true ? "True" : "False";

                config.Save(fileName);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
    }
}
